<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript">
	function PTP(){
		$("#main").hide();
		$("#PTP").fadeIn();
	}
	function Topic(){
		$("#main").hide();
		$("#Topic").fadeIn();
	}


	// PTP 
	function sendToAmq() {
		var text = $("#sendText").val();
		$.ajax({
			url : "amq/sendText.sls",
			type : "post",
			data : {
				"text" : text
			},
			dataType : "json",
			success : function(result) {
				$("#sendText").val("");
				$("#sendText").focus();

				$("#sendDiv").fadeIn();
				$("#sendSpan").html(result.msg);
				setTimeout(function() {
					$("#sendDiv").hide();
				}, 3000);
			},
			error : function() {
				window.location.href = "error.sls";
			}
		});
	}

	function recFromAmq() {
		$.ajax({
			url : "amq/recText.sls",
			type : "post",
			data : {},
			dataType : "json",
			success : function(result) {
				$("#recText").val(result.data);

				$("#recDiv").fadeIn();
				$("#recSpan").html(result.msg);
				setTimeout(function() {
					$("#recDiv").hide();
				}, 3000);
			},
			error : function() {
				window.location.href = "error.sls";
			}
		});
	}

	function recAll() {
		$.ajax({
			url : "amq/recAll.sls",
			type : "post",
			data : {},
			dataType : "json",
			success : function(result) {
				var datas = result.data;
				var s = '<tr>';
				for (var i = 0; i < datas.length; i++) {
					s += '<td>' + datas[i] + '</td>';
				}
				s += '</tr>';
				console.log(s);
				var $tr = $(s);
				$("#recAll").append($tr);
			},
			error : function() {
				window.location.href = "error.sls";
			}
		});
	}
	
	//Topic
	function sendTopic(){
		var sendTopicText = $("#sendTopicText").val();
		$.ajax({
			url : "amq/sendTopic.sls",
			type : "post",
			data : {"text":sendTopicText},
			dataType : "json",
			success : function(result) {
				alert(result.msg);
				$("#sendTopicText").val("");
			},
			error : function() {
				window.location.href = "error.sls";
			}
		});
	}
	
	function recTopic(){
		$.ajax({
			url : "amq/recTopic.sls",
			type : "post",
			data : {},
			dataType : "json",
			success : function() {

			},
			error : function() {
				window.location.href = "error.sls";
			}
		});
	}
	
	function testAMQ() {
		
    }
</script>
<!-- main -->
<div id="main">
	<div class="msg-a" onclick="PTP();">PTP</div>
	<div class="msg-b" onclick="Topic();">Topic</div>
</div>

<!-- PTP -->
<div id="PTP" style="display: none;">
	<div class="msg-a">
		<input id="sendText" type="text"> <input type="button"
			value="&nbsp;发  送&nbsp;" onclick="sendToAmq();">
	</div>


	<div class="msg-b">
		<input id="recText" type="text" readonly="readonly"> <input
			type="button" value="&nbsp;接  收&nbsp;" onclick="recFromAmq();">
	</div>


	<div class="msg-a">
		<input type="button" value="全部接受" onclick="recAll();">
	</div>
	<div class="table" id="emp_table">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tbody class="msg-a" id="recAll">

			</tbody>
		</table>
	</div>
	<div class="msg-a" style="display: none;" id="sendDiv">
		<span id="sendSpan"></span>
	</div>

	<div class="msg-b" style="display: none;" id="recDiv">
		<span id="recSpan"></span>
	</div>
</div>

<!-- Topic -->
<div id="Topic" style="display: none;">
	<div class="msg-a">
		<input id="sendTopicText" type="text"> 
		<input type="button" value="发布订阅" onclick="sendTopic();">
	</div>
	
	<div class="msg-b" onclick="recTopic();">订阅消息</div>
</div>